

project , uses(../data_output/legislators_klarner.dta)


	**********
	** South Dakota House
	** Consecutive term limites: 4 x 2-year terms.
	** First year: 2000		
	**********	

	use ../data_output/legislators_klarner.dta, clear
	keep if state=="SD" & chamber=="house"
	keep if regexm(term, "1997|1999|2001|2003|2005|2007|2009|2011|2013|2015")
	gen lastname = trim(regexs(1)) if regexm(lower(CandName),"([^,]+)")
	gen firstname = trim(regexs(1)) if regexm(lower(CandName),",( [^ ]+)")
	gen mergename = lastname
	bys lastname term: gen n = _n
	by lastname term: egen mean_n=mean(n)
	replace mergename = firstname + " " + lastname if mean_n>1 |  lastname == "olson" | lastname == "peterson"  | lastname == "novstrup" | lastname == "haggar" | lastname == "sutton" | lastname == "hennies"
	drop n mean_n
	save ../data_output/legislators_SD_house_1997_2016.dta, replace

	gen year_minus0 = year	
	gen year_minus2 = year-2
	gen year_minus4 = year-4
	gen year_minus6 = year-6
	tostring year_minus*, replace
	gen s = year_minus6+";" +year_minus4+";"+year_minus2+";"+ year_minus0 + ";"
	gen termlimited = regexm(elections_won, s)==1 & year>=1998		
	save ../data_output/legislators_SD_house_1997_2016.dta, replace
project, creates(../data_output/legislators_SD_house_1997_2016.dta)	
	
	
project, original(../data_input/SD/sd_bills.csv)
	import delimited using ../data_input/SD/sd_bills.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	tostring session , replace
	gen term = ""
	replace term = "1997-1998" if regexm(session, "1997|1998")
	replace term = "1999-2000" if regexm(session, "1999|2000")
	replace term = "2001-2002" if regexm(session, "2001|2002")
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2012")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")	
	gen mergename = regexs(1) if regexm(lower(name),"([^,]+)")
	replace mergename = regexs(1) + " " + mergename if regexm(lower(name),", ([^ ]+)") & [mergename=="brown" | mergename=="olson" | mergename=="peterson" | mergename == "novstrup" | mergename == "haggar" | mergename == "sutton"  | mergename == "hennies"]
	replace mergename = "vanetten" if mergename=="van etten"
	replace mergename = "vannorman" if mergename=="van norman"
	replace mergename = "fischerclemens" if mergename=="fischer-clemens"
	replace mergename = "ironcloud" if mergename=="iron cloud iii" & term=="2009-2010"
	replace mergename = "cloud" if mergename=="iron cloud iii" & term=="2011-2012"
	replace mergename = "vangerpen" if mergename=="van gerpen"
	replace mergename = "james peterson" if mergename=="jim peterson"
	replace mergename = "bill peterson" if mergename=="william peterson"
	replace mergename = "obrien" if mergename=="o'brien"
	replace mergename = "hueck" if mergename=="de hueck"
	replace mergename = "daniel sutton" if mergename=="dan sutton" & term=="1999-2000"
		
	merge m:1 mergename term using ../data_output/legislators_SD_house_1997_2016.dta
	//6 observations do not merge: hanks, jaspers, jenna haggar, rost, vangerpen
	drop if _merge==1
	gen law = regexm(lower(status) , "signed by gover|adopted" )
	gen sponsor = 1 if bill!="" &  type!="cosponsor"
	gen cosponsor = 1 if bill!="" &  type=="cosponsor"
	save ../data_output/bills_by_sponsor_SD_1997_2016.dta, replace
project , creates(../data_output/bills_by_sponsor_SD_1997_2016.dta)

	use ../data_output/bills_by_sponsor_SD_1997_2016.dta, clear
	collapse (sum) law sponsor cosponsor (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party mergename inoffice year termlimited vtsh, by(CandId term )
	
	save ../data_output/total_bills_by_sponsor_SD_1997_2016.dta, replace
project, creates(../data_output/total_bills_by_sponsor_SD_1997_2016.dta)		



	**Voting
	
	use ../data_output/legislators_klarner.dta, clear
	gen lastname = regexs(1) if regexm(lower(CandName),"([^,]+), [^!]")
	gen firstname = regexs(1) if regexm(lower(CandName),"[^,]+, ([^ ]+)")
	keep if state=="SD" & chamber=="house"
	keep if regexm(term, "2003|2005|2007|2009|2011|2013|2015")
	gen os_mergename = lastname
	bys  os_mergename term: gen n = _n
	by  os_mergename term: egen mean_n=mean(n)
	replace os_mergename = lastname + " (" + firstname +")"  if mean_n>1  //firstname  + " " +  lastname //firstname+ ". "
	drop n mean_n
	tempfile tmp
	save `tmp', replace
	
	
project, original(../data_input/SD/sd_votes.csv)	
	import delimited using ../data_input/SD/sd_votes.csv,  clear  delimiters(";") bindquotes(strict) encoding("utf-8") varnames(1)
	gen floor = regexm(location, "House of Representatives|House$")
	keep if chamber=="house"
	tostring session, replace
	gen term = ""
	replace term = "2003-2004" if regexm(session, "2003|2004")
	replace term = "2005-2006" if regexm(session, "2005|2006")
	replace term = "2007-2008" if regexm(session, "2007|2008")
	replace term = "2009-2010" if regexm(session, "2009|2010")
	replace term = "2011-2012" if regexm(session, "2011|2012")
	replace term = "2013-2014" if regexm(session, "2013|2014")
	replace term = "2015-2016" if regexm(session, "2015|2016")
	gen os_mergename = trim(lower(name))
	replace os_mergename = lower(os_mergename)
	replace os_mergename = subinstr(os_mergename,"’","",.)
	replace os_mergename = subinstr(os_mergename,"'","",.)
	replace os_mergename = subinstr(os_mergename,"-","",.)	
	replace os_mergename = "deadrick"	if os_mergename=="deadrick (thomas)" & regexm(term, "2003")
	replace os_mergename = "pederson"	if os_mergename=="pederson (gordon)" & regexm(term, "2003|2005|2007")
	replace os_mergename = "peterson (james)"	if os_mergename=="peterson (jim)" & regexm(term, "2003")
	replace os_mergename = subinstr(os_mergename, "van ","van",.) 
	replace os_mergename = "olson"	if os_mergename=="olson (ryan)" & regexm(term, "2005")
	replace os_mergename = "greenfield"	if os_mergename=="greenfield (brock)" & regexm(term, "2007|2009|2011|2013")
	replace os_mergename = "ironcloud"	if os_mergename=="iron cloud iii" & regexm(term, "2009")
	replace os_mergename = "jensen"	if os_mergename=="jensen (phil)" & regexm(term, "2007|2009|2011")
	replace os_mergename = "olson (betty)"	if os_mergename=="olson" & regexm(term, "2007|2009")
	replace os_mergename = "cloud"	if os_mergename=="iron cloud iii" & regexm(term, "2011")
	replace os_mergename = "hunhoff"	if os_mergename=="hunhoff (bernie)" & regexm(term, "2011|2013")
	replace os_mergename = "jones"	if os_mergename=="jones (tom)" & regexm(term, "2011")
	replace os_mergename = "otten"	if os_mergename=="otten (herman)" & regexm(term, "2013|2015")
	replace os_mergename = "peterson"	if os_mergename=="peterson (jim)" & regexm(term, "2013")
	replace os_mergename = "greenfield"	if os_mergename=="greenfield (lana)" & regexm(term, "2015")
	replace os_mergename = "jensen"	if os_mergename=="jensen (alex)" & regexm(term, "2015")
	replace os_mergename = "peterson"	if os_mergename=="peterson (kent)" & regexm(term, "2015")
	replace os_mergename = "haggar (don)"	if os_mergename=="haggar" & regexm(term, "2013")
	replace os_mergename = "haggar (jenna)"	if os_mergename=="netherton" & regexm(term, "2013")
	replace os_mergename = "haggar"	if os_mergename=="netherton" & regexm(term, "2011")
	replace os_mergename = "novstrup"	if os_mergename=="novstrup (david)" & regexm(term, "2009|2011|2013")
	replace os_mergename = "haverly"	if os_mergename=="haverly (jeffrey)" & regexm(term, "2007")
	replace os_mergename = "heineman"	if os_mergename=="heineman (phyllis)" & regexm(term, "2007")
	replace os_mergename = "hunhoff"	if os_mergename=="hunhoff (bernie)" & regexm(term, "2009")
	
	merge m:1  os_mergename term using `tmp'		
	
	// 2 obs not merging
	drop if _merge==1
	drop _merge
	gen floorvotes = 0 if floor==1
	replace floorvotes = 1 if regexm(vote, "Yea|Nay")==1 & floor==1
	gen cmtvotes = 0 if floor==0
	replace cmtvotes = 1 if regexm(vote, "Yea|Nay")==1 & floor==0
	replace vote = "1" if regexm( vote, "Yea")
	replace vote = "-1" if regexm( vote, "Nay")
	replace vote = "0" if vote!="1" & vote!="-1"
	destring vote, replace
	egen vote_id = group(bill motion date location)
	bys party vote_id : egen party_mean=mean(vote)
	gen party_vote = 0
	replace party_vote = 1 if party_mean>0
	replace party_vote = -1 if party_mean<0
	gen vote_party_line = vote==party_vote
	gen vote_against_party = vote==-party_vote
	
	*floor votes
	preserve
	
		keep if floor==1
		bys vote_id: egen votemargin = mean(vote)
		gen close_floorvotes = floorvotes if abs(votemargin)<.1			
		save ../data_output/floorvotes_SD_2003_2016.dta, replace
project, creates(../data_output/floorvotes_SD_2003_2016.dta)	
	
		use ../data_output/floorvotes_SD_2003_2016.dta, clear
		collapse  (mean) vote_party_line vote_against_party (sum) *floorvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party  inoffice year  vtsh, by(CandId term )
	
		save ../data_output/total_floorvotes_SD_2003_2016.dta, replace
project, creates(../data_output/total_floorvotes_SD_2003_2016.dta)	

	restore
	
	
	*cmt vote
	drop if floor==1
	save ../data_output/cmtvotes_SD_2003_2016.dta, replace	
project, creates(../data_output/cmtvotes_SD_2003_2016.dta)	
	
	use ../data_output/cmtvotes_SD_2003_2016.dta, clear
	collapse  (mean) vote_party_line vote_against_party (sum) cmtvotes (first) elections_* terms_in_office win TermLengthActual CandName district chamber state party  inoffice year  vtsh, by(CandId term )
	save ../data_output/total_cmtvotes_SD_2003_2016.dta, replace
project, creates(../data_output/total_cmtvotes_SD_2003_2016.dta)	
	

